package com.example.cheeseerpadmin.bus.service.impl;

import com.example.cheeseerpadmin.bus.entity.BusGoods;
import com.example.cheeseerpadmin.bus.entity.BusInport;
import com.example.cheeseerpadmin.bus.entity.BusOutport;
import com.example.cheeseerpadmin.bus.mapper.BusGoodsMapper;
import com.example.cheeseerpadmin.bus.mapper.BusInportMapper;
import com.example.cheeseerpadmin.bus.mapper.BusOutportMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cheeseerpadmin.bus.service.BusInportService;
import com.example.cheeseerpadmin.bus.service.BusOutportService;
import com.example.cheeseerpadmin.sys.common.WebUtils;
import com.example.cheeseerpadmin.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cheese
 * @since 2022-02-17
 */
@Service
@Transactional
public class BusOutportServiceImpl extends ServiceImpl<BusOutportMapper, BusOutport> implements BusOutportService {

    @Autowired
    private BusGoodsMapper busGoodsMapper;
    @Autowired
    private BusInportMapper busInportMapper;

    @Override
    public void addOutport(Integer id, Integer number, String remark) {
        //根据进货单id拿到进货单
        BusInport busInport = busInportMapper.selectById(id);
        busInport.setNumber(busInport.getNumber()-number);
        busInportMapper.updateById(busInport);
        //根据进货单拿到商品id
        BusGoods busGoods = busGoodsMapper.selectById(busInport.getGoodsid());
        busGoods.setNumber(busGoods.getNumber()-number);
        busGoodsMapper.updateById(busGoods);
        //创建一个退货单对象，然后往里面放值,最后直接插入表中即可
        BusOutport busOutport = new BusOutport();
        SysUser user = (SysUser)WebUtils.getSession().getAttribute("user");
        busOutport.setProviderid(busInport.getProviderid());
        busOutport.setPaytype(busInport.getPaytype());
        busOutport.setOutputtime(new Date());
        busOutport.setOperateperson(user.getName());
        busOutport.setOutportprice(busInport.getInportprice());
        busOutport.setNumber(number);
        busOutport.setRemark(remark);
        busOutport.setGoodsid(busInport.getGoodsid());
        getBaseMapper().insert(busOutport);
    }
}
